package com.quanyan.club.mq.producer.impl;


import com.google.gson.Gson;

import com.quanyan.club.mq.producer.PersonCenterTaskProducer;
import com.quanyan.mq.client.QcMsgClient;
import com.quanyan.mq.client.reqeust.MqRequestMsg;
import com.quanyan.mq.client.reqeust.SendCallbackListener;
import com.quanyan.mq.client.response.MqResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Nonnull;

/**
 * @Author Hugo.Wwg
 * @Since 2016-05-15
 */
@Service("personCenterTaskClient")
public class PersonCenterTaskClientImpl implements QcMsgClient {

    private Logger logger = LoggerFactory.getLogger(PersonCenterTaskClientImpl.class);

    @Autowired
    private PersonCenterTaskProducer personCenterTaskProducer;

    @Nonnull
    @Override
    public MqResponse syncSend(@Nonnull MqRequestMsg mqRequestMsg) {
        MqResponse mqResponse = personCenterTaskProducer.syncSend(mqRequestMsg);
        return mqResponse;
    }

    @Override
    public void asyncSend(@Nonnull MqRequestMsg mqRequestMsg) {
        logger.warn("SEND TO MQ : DATA : {}", new Gson().toJson(mqRequestMsg).toString());
        personCenterTaskProducer.asyncSend(mqRequestMsg, new SendCallbackListener() {

            @Override
            public void onSuccess(String sendResult) {
                logger.warn("SEND TO MQ : RESULT : {}", sendResult);
            }

            @Override
            public void onException(Throwable e) {
                logger.warn("SEND TO MQ EXCEPTION : {}", e.getMessage());
            }
        });
    }
}
